package toesp.util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn; 
/**
 *
 * @author satria
 */
public class JtableUtil {
    private JTable objJtable;
    
    public JtableUtil(){ 
    }
    
    public JtableUtil(JTable objJtable){
        this.objJtable = objJtable;
    }
    
    public void setJtable(JTable objJtable){
        this.objJtable = objJtable;
    }
    /**
    * Creates a formatJTable from JTable.
    * @param name
    * @return WritableWorkbook
    */
    
    public void formatJTable(Object[][] data,String[] columnNames,int[] columnWidth,int intAutoResizeMode,int intSelectionModel){
        try {
            final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
                                    JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
                                    JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
            
            final int SelectionModel[] = { ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
                                            ListSelectionModel.SINGLE_INTERVAL_SELECTION, 
                                            ListSelectionModel.SINGLE_SELECTION}; 
            
    
            this.objJtable.setAutoCreateRowSorter(true);
            this.objJtable.setSelectionMode(SelectionModel[intSelectionModel]);
            this.objJtable.setAutoResizeMode(modeKey[intAutoResizeMode]);  
            this.objJtable.setColumnSelectionAllowed(false);
            this.objJtable.setRowSelectionAllowed(true); 
            this.objJtable.setModel(new DefaultTableModel(data, columnNames));
            
            TableColumn column = null;
            for (int i = 0; i < columnNames.length; i++) { 
                column = this.objJtable.getColumnModel().getColumn(i);            
                column.setPreferredWidth(columnWidth[i]);   
            }  
        } catch (Exception e) {
            System.out.println("*Error : "+e.getMessage());
        }
    }
    
    /**
    * Creates a setContentJTable from JTable.
     * @param columnNames 
     * @param data 
     * @param name
    * @return WritableWorkbook
    */
    public void setContentJTable(String[] columnNames,Object[][] data){
        try {
            this.objJtable.setModel(new DefaultTableModel(data, columnNames));
        } catch (Exception e) {
        
        }
    }
    
    public void setValueAtJTable(Object objNewValue,int intRow,int intCol){
        String strResult = "";
        
        try {             
            this.objJtable.setValueAt(objNewValue, intRow, intCol);
        } catch (Exception e) {
        
        } 
    }
    
    public Object getValueAt(int intRow, int intCol) {
        return this.objJtable.getValueAt(intRow, intCol);
    }

    
    public Object[] getRowData(){
        Object[] objResult = new Object[this.getColoumnCount()];
        
        try {             
             int intRow = this.objJtable.getSelectedRow();
             
             for (int i = 0; i < objResult.length; i++) {
                objResult[i] = this.objJtable.getValueAt(intRow, 0);                
            }
        } catch (Exception e) {
        
        }         
       
        return objResult;
    }
    
    public int getColoumnCount(){
        int intResult = 0;
        
        
        try {             
             intResult = this.objJtable.getColumnCount();
        } catch (Exception e) {
        
        }
        
        return intResult;
    }
    
    public int getRowCount(){
        int intResult = 0;
        
        try {             
             intResult = this.objJtable.getRowCount();
        } catch (Exception e) {
        
        }
        
        return intResult;
    } 
    
    public void MoveNextFlex(JTable objJtable,JButton tombolPrev,JButton tombolNext,int intBaris){
    
//        int intRows;
//        
//        
//        intRows = intBaris+1;
//        
//        if(intRows==objJtable.getRowCount()){
//            tombolPrev.setEnabled(true);
//            tombolNext.setEnabled(false);
//        }else{
//            tombolPrev.setEnabled(true);
//            tombolNext.setEnabled(true);
//        }
//        
//        objJtable.setRowSelectionInterval(5, 5);
//        JOptionPane.showMessageDialog(null, "");
//        Static Akhir As Long
//    
//    On Error GoTo errHandle
//    
//    Akhir = intBaris
//    Akhir = Akhir + 1
//    
//    If Akhir = FlexGrid.Rows Then
//        TombolPrev.Enabled = True
//        TombolNext.Enabled = False
//        Exit Sub
//        
//    Else
//        TombolPrev.Enabled = True
//        TombolNext.Enabled = True
//    End If
//    
//    FlexGrid.Row = Akhir
//    FlexGrid.TopRow = Akhir
//    
//    Exit Sub
//errHandle:
//    Select Case Err.Number
//        Case 381
//            TombolPrev.Enabled = False
//            TombolNext.Enabled = False
//        Case Else
//            Resume Next
//    End Select
    }
    
    public void MovePreviousFlex(){}
}
